<?php


namespace app\index\controller;


use think\Validate;

class Wallet extends Base
{
    /**
     * 我的钱包总金额
     */
    public function index()
    {
        $id = $this->user['id'];
        $type = input('post.type');
        $page = input('post.page');

//        if ($type == 1) { //提现记录
//            $data['record'] = db('tixian')
//                ->whereIn('us_id', $id)
//                ->order('id', 'desc')
//                ->field('tx_apply_time,tx_type,tx_num,tx_status')
//                ->paginate(10, true, ['type' => 'bootstrap',
//                    'var_page' => 'page',
//                    'page' => $page]);
//            $tx_num = db('tixian')->whereIn('us_id',$id)->count();
//            $tx_page_num = ceil($tx_num / 10);
//            $data['tx_page'] = $tx_page_num;
//        }
//        if ($type == 2) {
//            $data['mx'] = db('wallet')
//                ->whereIn('us_id', $id)
//                ->order('id', 'desc')
//                ->field('wa_note,add_time,wa_num,s_id,wallet_type')
////                ->select();
//                ->paginate(10, true, ['type' => 'bootstrap',
//                'var_page' => 'page',
//                'page' => $page]);
//            //获取订单id
////            $this->s_msg(null, $data);
////            var_dump($data['mx']);exit();
////            $or_id =db('order')->whereIn('us_id',$id)->field('id')->select();
////            foreach ($or_id as $k=>$v){
////                    $list[] = db('order_detail')
////                        ->whereIn('id',$v['id'])
////                        ->field('ca_id,or_pd_name,or_pd_total')
////                        ->paginate(10, true, ['type' => 'bootstrap',
////                            'var_page' => 'page',
////                            'page' => $page]);
//////                        ->select();
////            }
//////            var_dump($list);
////            foreach ($list as $k=>$v){
////                foreach ($v as $value)
////                    $data['mx'][] = $value;
////            }
////            var_dump($dat);
////            exit();
////            var_dump($list);exit();
////            $data['or'] = db('order_detail')
////                ->whereIn('id',$or_id)
////                ->field('ca_id,or_pd_name,or_pd_total')
////                ->select();
//
////            $data['mx'] =
////            $list = $data['mx'];
////            foreach ($list as $k=>$v) {
//////                echo $list[$k];
////               $list[$k]['s_id_name'] = db('user')->where('id', $v['s_id'])->value('us_nickname');
////            }
////            exit();
//            $mx_num = db('wallet')->whereIn('us_id',$id)->count();
//            $mx_page_num = ceil($mx_num / 10);
//            $data['mx_page'] = $mx_page_num;
//        }
        if ($type == 1) {    //  提现记录
            $data['tx'] = model('MoneyDetailed')
                ->whereIn('us_id', $id)
                ->order('id', 'desc')
                ->where(['type'=>3,'tx_people_role'=>1])
                ->field('add_time,tx_status,money')
                ->limit(10)->page($page)
                ->select();
        }
        if ($type == 2) {   // 钱包明细  1. 消费  2.团队奖
            $data['money'] = model('MoneyDetailed')
                ->whereIn('us_id', $id)
                ->where(['type'=>[1,2]])
                ->order('id', 'desc')
                ->field('add_time,type,money,origin_id')
                ->limit(10)->page($page)
                ->select();

            foreach ($data['money'] as $k=>$v){
                $data['money'][$k]['origin_id'] = db('user')->where('id',$v['origin_id'])->value('us_nickname');
            }
        }
        $data['us_all_get'] = $this->user['us_all_get'];
        $data['us_all_two_money'] = $this->user['us_all_two_money'];
        $this->s_msg(null, $data);
}

    /**
     * 申请提现
     */
    public function withdraw()
    {
        $u_id = $this->user['id'];
        $withdraw_num = input('post.tx_num');  // 提现金额
        $us_Bank = model('banks')->where('us_id',$u_id)->where('type',1)->where('status',1)->find();
        if (empty($us_Bank)){
            $this->e_msg('请添加银行卡',3);
        }
        $withdraw_type = $us_Bank['id'];   // 提现银行卡id

        $pay_pwd = input('post.pay_pwd');    //交易密码
//        $pay_num = $this->user['us_all_get'];    // 钱包余额
        if (empty($withdraw_num) || $withdraw_num < 0) {
            $this->e_msg('提现金额不能为空、负数、0');
        }
        if (!(ctype_digit($withdraw_num))) {
            $this->e_msg('提现金额必须为整数');
        }
        if ($withdraw_num > $this->user['us_all_get']) {
            return $this->e_msg('钱包余额不足，请重新填写');
        }

//        if ((!empty($withdraw_type))||$withdraw_type ==1)
//        {
//            $id = $this->user['id'];
//           $we_account = db('user')->where('id',$id)->value('we_account');
//           if (empty($we_account)){
//               return $this->e_msg('未填写支付宝号，请填写后再次提现');
//           }
//        }
//        if ((!empty($withdraw_type))||$withdraw_type ==2)
//        {
//            $id = $this->user['id'];
//            $we_account = db('user')->where('id',$id)->value('ali_account');
//            if (empty($we_account)){
//                return $this->e_msg('未填写微信号，请填写后再次提现');
//            }
//        }

//        if (empty($withdraw_type)){
//            $this->e_msg('提现账户必须填写');
//        }
//        // 查询是否有此账户
//        $checkBank =  model('Banks')->where(['id'=>$withdraw_type,'us_id'=>$u_id,'type'=>1])->find();
//        if (!$checkBank){
//            $this->e_msg('参数错误');
//        }

        if (encrypt($pay_pwd) != $this->user['us_pay_pwd']) {
            return $this->e_msg('交易密码错误，请重新填写');
        }
        // 提现账户
//        $tx_account = model('user')->where('id',$u_id)->field('ali_account,we_account')->find();
//        if ($withdraw_type == 1) {
//            $t_account = $tx_account['we_account'];
//        }
//        if ($withdraw_type == 2) {
//            $t_account = $tx_account['ali_account'];
//        }

            $insert = model('user')
                ->where('id', $u_id)
                ->setDec('us_all_get', $withdraw_num);
            $us_all_get = model('user')->where('id',$u_id)->find();

        $data = [
            'us_id' => $u_id, //用户id
            'type'=>3,   // 1. 消费  2.团队奖 3. 提现
            'y_pay_money'=>$this->user['us_all_get'],  // 提现前可交易余额
            'y_frozen_money'=>$this->user['us_all_two_money'], //提现前冻结金额
            'money' => $withdraw_num,  // 提现金额
            'over_pay_money'=>$us_all_get->us_all_get, // 提现后可用余额
            'over_frozen_money'=>$us_all_get->us_all_two_money, // 剩余冻结金额
            'add_time' => date('Y-m-d H:i:s'), //申请时间
            'tx_status' => 0, //提现状态
            'tx_people_role'=>1,  //1 用户提现  2 供应商提现
            'tx_account_id' => $withdraw_type, //提现银行卡ID
            'explain'=>'提现'
        ];
         $in = db('MoneyDetailed')->insert($data);
            if ($in && $insert) {
                return $this->s_msg('您的提现申请已提交，等待管理员审核');
            } else {
                return $this->e_msg('提交超时');
            }
        }


    /**
     * 提现记录
     */
//    public function annal()
//    {
//        $id = $this->user['id'];
//        $data = db('tixian')
//            ->whereIn('us_id', $id)
//            ->order('id', 'desc')
//            ->field('tx_apply_time,tx_type,tx_num,tx_status')
//            ->paginate(1,false,$config = ['query'=>request()->param()]);
////            ->select();
//        if ($data) {
//            return $this->s_msg('成功', $data);
//        } else {
//            return $this->s_msg('失败');
//        }
//
//    }

    /**
     * 钱包明细
     */
//    public function detail()
//    {
//        $id =$this->user['id'];
//        $data = db('wallet')->whereIn('us_id',$id)->field('wa_note,add_time,wa_num,s_id')->select();
//        foreach ($data as $k=>$v)
//        {
//            $data[$k]['s_id'] = db('user')->where('id',$v['s_id'])->value('us_nickname');
//        }
//        if ($data) {
//            return $this->s_msg('成功', $data);
//        } else {
//            return $this->s_msg('失败');
//        }
//
//    }


}